package com.ai.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: springboot3-demos
 * @description:
 * @author: Reagan Li
 * @create: 2024-06-12 15:10
 **/
@RestController
@Slf4j
@RequestMapping("/ai")
public class FunctionController {

    @Autowired
    private OpenAiChatModel chatModel;

    @GetMapping("/ask")
    public String ask(@RequestParam("msg") String msg) {
        UserMessage userMessage = new UserMessage(msg);

        ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
                OpenAiChatOptions.builder().withFunction("weatherFunction").build())); // (1) Enable the function

        log.info("Response: {}", response);
        return response.getResult().getOutput().getContent();
    }
}
